package com.iemes.controller.hr;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.CodeFormMap;
import com.iemes.entity.DepartmentForMap;
import com.iemes.entity.DockFormMap;
import com.iemes.entity.MealFormMap;
import com.iemes.entity.PositionFormMap;
import com.iemes.entity.RecordFormMap;
import com.iemes.entity.SocialFormMap;
import com.iemes.entity.StaffFormMap;
import com.iemes.mapper.hr.DepartmentinfoMapper;
import com.iemes.mapper.hr.RecordMapper;
import com.iemes.mapper.hr.SocialMapper;
import com.iemes.mapper.hr.StaffMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;
import com.iemes.util.DateUtils;

@Controller
@RequestMapping("/hrstaff/")
public class StaffController extends BaseController {

	@Inject
	private StaffMapper staffMapper;
	@Inject
	private RecordMapper recordMapper;
	@Inject
	private DepartmentinfoMapper departmentinfoMapper;
	@Inject
	private SocialMapper socialMapper;
	

	// 查看人员
	@RequestMapping("staffAttend")
	public String examineOrder(Model model) {
		DepartmentForMap departmentForMap = getFormMap(DepartmentForMap.class);
		departmentForMap.put("where", "where id!=16 ");
		// 查部门名称
		List<DepartmentForMap> department = departmentinfoMapper
				.findByWhere(departmentForMap);
		model.addAttribute("caset", department);
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/hr/staff_list";
	}

	// 人员分页
	@ResponseBody
	@RequestMapping("staffinfomation_findByPage")
	public PageView findByPage(String pageNow, String pageSize)
			throws Exception {
		StaffFormMap staffFormMap = getFormMap(StaffFormMap.class);

		List<Map<String, Object>> staffFormMaps = staffMapper
				.getAllInfoation(staffFormMap);
		//自动转换社保是否购买
		for(Map map:staffFormMaps){
			String pId=map.get("id").toString();
			String social_security=map.get("social_security").toString();
			if(social_security.equals("1")){
				Date dt=new Date();
				SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
				Calendar rightNow = Calendar.getInstance();
				 rightNow.setTime(dt); 
				 rightNow.add(Calendar.MONTH,-1);//当前时间的月份减一
				 String nowDateTimes=sdf.format( rightNow.getTime());//格式化
				 //截取年月日
				 String nowDateTime=nowDateTimes.substring(0,7);
				 System.out.println(nowDateTime);
				 SocialFormMap socialFormMape = getFormMap(SocialFormMap.class);
				 socialFormMape.put("Nowrecord_date",nowDateTime);
				 socialFormMape.put("pId", pId);
				 List<Map<String,Object>> mealFormMaps=socialMapper.getAllSocial(socialFormMape);//通过名字和日期去查询是否有社保记录
				 //判断是否为空
				 if(mealFormMaps.isEmpty()||mealFormMaps==null){
					 continue;
				 }else{
					DockFormMap  doks= staffMapper.findbyFrist("id", pId, DockFormMap.class);
					String social_id= doks.get("social_id").toString();
					 SocialFormMap socialFormMapr = getFormMap(SocialFormMap.class);
					 socialFormMapr.put("record_date", nowDateTime);
					 socialFormMapr.put("social_id", social_id);
					 SocialFormMap SocialFormMapw=socialMapper.getsocalAll(socialFormMapr);
					 String conditions=SocialFormMapw.get("conditions").toString();
					 if(conditions.equals("2")){//判断是增是减否则会出现异常
						 StaffFormMap staffFormMapq = getFormMap(StaffFormMap.class);
						 staffFormMapq.put("id", pId);
						 staffFormMapq.put("social_security", 2);
						 staffMapper.editEntity(staffFormMapq);
					 }
					
					
				 }
			}else{

				Date dt=new Date();
				SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
				Calendar rightNow = Calendar.getInstance();
				 rightNow.setTime(dt); 
				 rightNow.add(Calendar.MONTH,-1);//当前时间的月份减一
				 String nowDateTimes=sdf.format( rightNow.getTime());//格式化
				 //截取年月日
				 String nowDateTime=nowDateTimes.substring(0,7);
				 SocialFormMap socialFormMape = getFormMap(SocialFormMap.class);
				 socialFormMape.put("Nowrecord_date",nowDateTime);
				 socialFormMape.put("pId", pId);
				 List<Map<String,Object>> mealFormMaps=socialMapper.getAllSocial(socialFormMape);//通过名字和日期去查询是否有社保记录
				 //判断是否为空
				 if(mealFormMaps.isEmpty()||mealFormMaps==null){
					 continue;
				 }else{
						DockFormMap  doks= staffMapper.findbyFrist("id", pId, DockFormMap.class);
						String social_id= doks.get("social_id").toString();
						 SocialFormMap socialFormMapr = getFormMap(SocialFormMap.class);
						 socialFormMapr.put("record_date", nowDateTime);
						 socialFormMapr.put("social_id", social_id);
					 SocialFormMap SocialFormMapw=socialMapper.getsocalAll(socialFormMapr);
					 String conditions=SocialFormMapw.get("conditions").toString();
					 if(conditions.equals("1")){//判断是增是减否则会出现异常
					 StaffFormMap staffFormMapq = getFormMap(StaffFormMap.class);
					 staffFormMapq.put("id", pId);
					 staffFormMapq.put("social_security", 1);
					 staffMapper.editEntity(staffFormMapq);
					 }
					
				 }
			
			}
		}
		
		LuogicPaging(pageNow, pageSize, staffFormMaps);
		pageView.setRecords(staffFormMaps);
		return pageView;
	}

	// 编辑员工信息
	@RequestMapping("staff_edit")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		PositionFormMap positionFormMap = getFormMap(PositionFormMap.class);
		if (Common.isNotEmpty(id)) {
			// 查询职务名称
			positionFormMap.put("where", "where departid!=16");
			List<PositionFormMap> position = recordMapper
					.findByWhere(positionFormMap);
			model.addAttribute("duty", position);
			StaffFormMap staffFormMaps = staffMapper.findbyFrist("id", id,
					StaffFormMap.class);
			model.addAttribute("staff", staffFormMaps);

		}

		return Common.BACKGROUND_PATH + "/hr/staff_edit";
	}

	// 修改员工信息
	@ResponseBody
	@RequestMapping(value = "staff_editEntity")
	@SystemLog(module = "员工信息数据维护", methods = "查看员工信息-修改员工信息")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity() {
		StaffFormMap staffFormMap = getFormMap(StaffFormMap.class);
		try {
			recordMapper.editEntity(staffFormMap);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "false";
		}
		return "success";
	}

	// 新增员工页面
	@RequestMapping("staff_add")
	public String addUI(Model model) throws Exception {
		// 工号
		String number = "TY" + DateUtils.getDateNumber();
		model.addAttribute("number", number);
		PositionFormMap positionFormMap = getFormMap(PositionFormMap.class);
		// 查询职务名称
		positionFormMap.put("where", "where departid!=16");
		List<PositionFormMap> position = recordMapper
				.findByWhere(positionFormMap);
		model.addAttribute("duty", position);
		return Common.BACKGROUND_PATH + "/hr/staff_add";
	}

	// 新增员工操作
	@ResponseBody
	@RequestMapping("staff_addEntity")
	@SystemLog(module = "员工管理", methods = "员工管理-新增员工")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntity() {

		StaffFormMap staffFormMap = getFormMap(StaffFormMap.class);
		DockFormMap dockFormMap = getFormMap(DockFormMap.class);
		String number = getPara("staffFormMap.job_number");
		try {
			// 创建时间
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String timess = df.format(new Date());
			staffFormMap.put("current_time", timess);
			staffMapper.addEntity(staffFormMap);
			StaffFormMap Staff = staffMapper.findbyFrist("job_number", number,
					StaffFormMap.class);
			String idc = Staff.get("id").toString();
			dockFormMap.put("person_id", idc);
			staffMapper.addEntity(dockFormMap);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return "false";
		}
		return "success";
	}

	// 删除员工
	@ResponseBody
	@RequestMapping("staff_deleteEntity")
	@Transactional(readOnly = false)
	// 需要事务操作必须加入此注解
	@SystemLog(module = "员工管理", methods = "员工管理-删除员工")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		StaffFormMap staffFormMap = getFormMap(StaffFormMap.class);
		String[] ids = getParaValues("ids");
		String Pid = null;
		for (String id : ids) {
			Pid = id;
			// orderMapper.deleteOrder(Integer.valueOf(id));
		}
		String[] value = Pid.split(",");
		for (String string : value) {
			// 删员工（修改状态码）
			staffMapper.deleteOrder(Integer.valueOf(string));
			staffFormMap = staffMapper.findbyFrist("id", string,
					StaffFormMap.class);
			String archives_id = (String) staffFormMap.get("archives_id");
			// 删档案（修改状态码）
			staffMapper.upRecord(archives_id);
		}
		return "success";
	}

	// 新增档案页面
	@RequestMapping("record_add")
	public String addUIs(Model model) throws Exception {
		String id = getPara("id");
		StaffFormMap staffFormMaps = staffMapper.findbyFrist("id", id,
				StaffFormMap.class);
		String value = (staffFormMaps.get("name").toString()).substring(0, 1);
		CodeFormMap codeFormMap = staffMapper.findbyFrist("value", value,
				CodeFormMap.class);
		String key = codeFormMap.get("key").toString();
		String file_number = key;
		// 通过档案外键查询档案号的最大值
		RecordFormMap recordFormMap = staffMapper
				.getAllRecordFormMap(file_number);
		String file_numbers = recordFormMap.get("file_number").toString();
		Integer number = Integer.valueOf(file_numbers.substring(
				file_numbers.indexOf("-") + 1, file_numbers.length()));
		String prefix = file_numbers
				.substring(0, file_numbers.indexOf("-") + 1);
		String record_File_number;
		if (number < 9) {
			record_File_number = prefix + "00" + (number + 1);
		} else if (number < 99) {
			record_File_number = prefix + "0" + (number + 1);
		} else {
			record_File_number = prefix + (number + 1);
		}

		PositionFormMap positionFormMap = getFormMap(PositionFormMap.class);
		// 查询职务名称
		positionFormMap.put("where", "where departid!=16");
		List<PositionFormMap> position = recordMapper
				.findByWhere(positionFormMap);
		model.addAttribute("staff", staffFormMaps);
		model.addAttribute("record_File_number", record_File_number);
		model.addAttribute("duty", position);
		return Common.BACKGROUND_PATH + "/hr/record_add";
	}

	// 新增档案操作
	@ResponseBody
	@RequestMapping("record_addEntity")
	@SystemLog(module = "档案管理", methods = "档案管理-新增档案")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntitys() {
		StaffFormMap staffFormMap = getFormMap(StaffFormMap.class);
		RecordFormMap recordFormMap = getFormMap(RecordFormMap.class);
		try {
			staffMapper.editEntity(staffFormMap);
			staffMapper.addEntity(recordFormMap);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return "false";
		}
		return "success";
	}

	// 员工社保情况
	@ResponseBody
	@RequestMapping("social_addEntity")
	@Transactional(readOnly = false)
	// 需要事务操作必须加入此注解
	@SystemLog(module = "社保管理", methods = "社保管理-员工社保更改")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String socialaddEntity() throws Exception {
		// 收费表
		DockFormMap dockFormMap = getFormMap(DockFormMap.class);

		// 创建时间
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String timess = df.format(new Date());

		String[] ids = getParaValues("ids");
		String Pid = null;
		for (String id : ids) {
			Pid = id;
		}
		String[] value = Pid.split(",");
		for (String string : value) {
			StaffFormMap staffFors=staffMapper.findbyFrist("id", string, StaffFormMap.class);
			String social_security=staffFors.get("social_security").toString();
			// 通过员工id查询出对应的收费表id
			List<DockFormMap> docks = staffMapper.findByAttribute("person_id", string,DockFormMap.class);
			DockFormMap dock=null;
			for(DockFormMap dockk: docks){
				dock=dockk;
			}
			if(social_security.equals("1")){
				SocialFormMap socialFormMape = getFormMap(SocialFormMap.class);
				socialFormMape.put("pId", string);
				String datetimea=timess.substring(0,7);
				socialFormMape.put("Nowrecord_date",datetimea);//把当前日期的年月日放入 进行模糊查询
				List<Map<String,Object>> mealFormMaps=socialMapper.getAllSocial(socialFormMape);//通过名字和日期去查询是否有社保记录
				if(mealFormMaps.isEmpty()||null==mealFormMaps){
					// 社保编号最大值
					Integer socialMax = staffMapper.getAllSocial();
					if(socialMax==null){
						socialMax=0;
					}
					socialMax += 1;
					// 社保
					SocialFormMap socialFormMap = getFormMap(SocialFormMap.class);
					socialFormMap.put("id", socialMax);
					socialFormMap.put("record_date", timess);
					socialFormMap.put("conditions", 2);
					Integer social_id=Integer.valueOf(string);
					social_id+=1;
					socialFormMap.put("social_id",social_id);
					staffMapper.addEntity(socialFormMap);
					String dockId = dock.get("id").toString();
					// 给收费对象id 和社保编号
					dockFormMap.put("id", dockId);
					dockFormMap.put("social_id", social_id);
					// 刚创建的社保直接把社保加入收费表中
					staffMapper.editEntity(dockFormMap);
				}else{
					continue;
				}
				
			}else{
				SocialFormMap socialFormMape = getFormMap(SocialFormMap.class);
				socialFormMape.put("pId", string);
				String datetimea=timess.substring(0,11);
				socialFormMape.put("Nowrecord_date",datetimea);//把当前日期的年月日放入 进行模糊查询
				List<Map<String,Object>> mealFormMaps=socialMapper.getAllSocial(socialFormMape);//通过名字和日期去查询是否有社保记录
				if(mealFormMaps.isEmpty()||null==mealFormMaps){
					// 社保编号最大值
					Integer socialMax = staffMapper.getAllSocial();
					if(socialMax==null){
						socialMax=0;
					}
					socialMax += 1;
					// 社保
					SocialFormMap socialFormMap = getFormMap(SocialFormMap.class);
					socialFormMap.put("id", socialMax);
					socialFormMap.put("record_date", timess);
					socialFormMap.put("conditions", 1);
					Integer social_id=Integer.valueOf(string);
					social_id+=1;
					socialFormMap.put("social_id",social_id);
					staffMapper.addEntity(socialFormMap);
					String dockId = dock.get("id").toString();
					// 给收费对象id 和社保编号
					dockFormMap.put("id", dockId);
					dockFormMap.put("social_id", social_id);
					// 刚创建的社保直接把社保加入收费表中
					staffMapper.editEntity(dockFormMap);
				}else{
					continue;
				}
			
			}

		}
		return "success";
	}

	// 员工餐费新增页面
	@RequestMapping("meal_add")
	public String addUImeal(Model model) throws Exception {
		String id = getPara("id");
		model.addAttribute("ids", id);
		return Common.BACKGROUND_PATH + "/hr/diner_add";
	}

	// 员工餐费新增操作
	@ResponseBody
	@RequestMapping("meal_addEntity")
	@SystemLog(module = "餐费管理", methods = "餐费管理-新增餐费")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String addmealEntity() {
		    MealFormMap mealFormMap=getFormMap(MealFormMap.class);
		    DockFormMap dockFormMap=getFormMap(DockFormMap.class);
		    // 创建时间
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String timess = df.format(new Date());
			//查询餐费表最大编号
			Integer mealMax=staffMapper.getAllMeall();
			if(mealMax==null){
				mealMax=0;
			}
			mealMax+=1;
			mealFormMap.put("record_date", timess);
			mealFormMap.put("id", mealMax);
		try {
			
			//新增餐费数据
			staffMapper.addEntity(mealFormMap);
            String id=getPara("id");
            // 通过员工id查询出对应的收费表
   			DockFormMap dock = staffMapper.findbyFrist("person_id", id,DockFormMap.class);
   			String dockId =dock.get("id").toString();
   			dockFormMap.put("id", dockId);
   			dockFormMap.put("meal_id", mealMax);
   			//添加餐费
   			staffMapper.editEntity(dockFormMap); 			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return "false";
		}
		return "success";
	}

	
	// 员工餐费删除操作
		@ResponseBody
		@RequestMapping("meal_deleteEntity")
		@SystemLog(module = "餐费管理", methods = "餐费管理-删除餐费")
		// 凡需要处理业务逻辑的.都需要记录操作日志
		public String deletemealEntity() {
			    MealFormMap mealFormMap=getFormMap(MealFormMap.class);
			    DockFormMap dockFormMap=getFormMap(DockFormMap.class);

			try {
				String[] ids = getParaValues("ids");
				String Pid = null;
				for (String id : ids) {
					Pid = id;
					// orderMapper.deleteOrder(Integer.valueOf(id));
				}
				String[] value = Pid.split(",");
				for (String string : value) {
					
				    // 创建时间
					SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					String timess = df.format(new Date());
					mealFormMap.put("record_date", timess);
		            // 通过员工id查询出对应的收费表
		   			DockFormMap dock = staffMapper.findbyFrist("person_id", string,DockFormMap.class);
		   			String dockId =dock.get("id").toString();
		   			String meal_id= dock.get("meal_id").toString();		   			
		   			dockFormMap.put("id", dockId);
		   		    //删除收费表的餐费的外键id
		   			staffMapper.updaMealOrder(dockId);
		   			//删除餐费表
		   			staffMapper.deleteMealOrder(meal_id);
		   			
				}
			
			} catch (Exception e) {
				// TODO Auto-generated catch block
				return "false";
			}
			return "success";
		}
	
}
